package com.example.curriculum.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.example.curriculum.R;
import com.example.curriculum.bean.ExpenseBean;
import com.example.curriculum.utils.SharepreferenceCacheUtils;
import com.example.curriculum.utils.Constans;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class ExpenseFragment extends Fragment
{
	protected static final String	TAG	= "ExpenseFragment";

	private ListView				mExpenseContainer;

	private List					mExpenseDatas;

	private Context					mContext;

	public ExpenseFragment(Context context) {
		this.mContext = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		// TODO 填充布局

		View view = View.inflate(getActivity(), R.layout.fragment_expense, null);

		mExpenseContainer = (ListView) view.findViewById(R.id.lv_expense_container);
		// mGuitarContainer.addHeaderView(View.inflate(getActivity(),
		// R.layout.class_hour_bar, null));
		return view;

	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState)
	{
		// TODO 加载数据？
		super.onActivityCreated(savedInstanceState);
		initData();
	}

	private void initData()
	{
		mExpenseDatas = new ArrayList();

		// 去网络加载数据
		loadNetData();

		mExpenseContainer.setAdapter(new GuitarListViewAdapter());
	}

	/**
	 * 加载网络数据
	 */
	private void loadNetData()
	{
		// TODO 未完善
		final String url = Constans.SERVER_URL;

		String json = SharepreferenceCacheUtils.getString(mContext, url);
		if (!TextUtils.isEmpty(json))
		{
			processData(json);
		}

		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo)
			{
				String result = responseInfo.result;

				Log.d(TAG, "网络数据正确返回 : " + result);

				// 存缓存
				SharepreferenceCacheUtils.setString(mContext, url, result);

				// 处理数据
				processData(result);
			}

			@Override
			public void onFailure(HttpException error, String msg)
			{
				Log.d(TAG, "网络数据失败 : " + msg);
			}
		});

	}

	/**
	 * 处理数据 解析 显示
	 * 
	 * @param json
	 */
	private void processData(String json)
	{
		// TODO 未完善
		// 1. json串的解析
		Gson gson = new Gson();
		ExpenseBean bean = gson.fromJson(json, ExpenseBean.class);

	}

	class GuitarListViewAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return 15;
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null)
			{
				holder = new ViewHolder();
				convertView = View.inflate(getActivity(), R.layout.item_expense, null);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();

			}
			return convertView;
		}

	}

	class ViewHolder
	{

	}
}
